package com.yjq.client;

import com.yjq.dao.RectangleDao;
import com.yjq.dao.TriangleDao;
import com.yjq.dao.impl.RectangleDaoImpl;
import com.yjq.dao.impl.TriangleDaoImpl;
import com.yjq.pojo.Rectangle;
import com.yjq.pojo.Triangle;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

/**
 * @author 尹嘉琪
 * @create 2021-12-19 15:09
 * 技术： swing + druid  + mysql
 */
public class MyFrame extends JFrame {

    RectangleDao rectangleDao = new RectangleDaoImpl();
    TriangleDao triangleDao = new TriangleDaoImpl();

    JPanel panel;

    double width;
    double height;
    double hypotenuse;

    JLabel label = new JLabel();
    JLabel widthLabel = new JLabel("请输入边长：");
    JTextField widthField = new JTextField(5);
    JTextField heightField = new JTextField(5);
    JTextField hypotenuseField = new JTextField(5);
    JButton buttonR = new JButton("确认");
    JButton buttonT = new JButton("确认");
    JLabel showLabel = new JLabel();
    JLabel showCollections;
    JComboBox<String> comboBox = new JComboBox<>();
    Control control = new Control();

    public MyFrame(String title) throws HeadlessException {

        panel = new JPanel();
        this.setContentPane(panel);

        label.setText("请选择要计算的图形：");
        label.setFont(new Font("微软雅黑", Font.BOLD, 20));
        widthLabel.setFont(new Font("微软雅黑", Font.BOLD, 15));
        widthField.setSize(100, 100);
        panel.setLayout(new MyLayout());

        comboBox.addItem("");
        comboBox.addItem("矩形");
        comboBox.addItem("三角形");
        comboBox.addItem("其他");
        comboBox.addItem("查询");

        showLabel.setLocation(0, 0);

        panel.add(showLabel);
        panel.add(label);
        panel.add(comboBox);

        comboBox.addActionListener((e)->{
            int selectedIndex = comboBox.getSelectedIndex();
            switch (selectedIndex) {
                case 0:
                    widthField.setVisible(false);
                    heightField.setVisible(false);
                    hypotenuseField.setVisible(false);
                    showLabel.setVisible(false);
                    widthLabel.setVisible(false);
                    buttonT.setVisible(false);
                    buttonR.setVisible(false);
                    this.setVisible(true);
                    break;
                case 1:
                    printRectangle();
                    control.setVisible(true);
                    this.setVisible(true);
                    break;
                case 2:
                    printTriangle();
                    this.setVisible(true);
                    break;
                case 3:
                    widthField.setVisible(false);
                    heightField.setVisible(false);
                    hypotenuseField.setVisible(false);
                    widthLabel.setVisible(false);
                    buttonT.setVisible(false);
                    buttonR.setVisible(false);
                    showLabel.setText("其他形状正在开发中,敬请期待！");
                    showLabel.setVisible(true);
                    this.setVisible(true);
                    break;
                case 4:
                    widthField.setVisible(false);
                    heightField.setVisible(false);
                    hypotenuseField.setVisible(false);
                    widthLabel.setVisible(false);
                    buttonT.setVisible(false);
                    buttonR.setVisible(false);
                    String s ="";
                    List<Rectangle> rectangles = rectangleDao.queryListForAll();
                    List<Triangle> triangles = triangleDao.queryListForAll();
                    for (Rectangle r : rectangles) {
                        s += r.getShape() + ":[" + r.getWidth() + "," + r.getHeight() + "]";
                    }
                    for (Triangle t : triangles) {
                        s += t.getShape() + ":[" + t.getWidth() + "," + t.getWidth() + "," + t.getHypotenuse() + "]";
                    }
                    if (triangles.size() == 0 && rectangles.size() == 0) {
                        showLabel.setText("当前没有数据");
                    } else {
                        showLabel.setText(s);
                        showLabel.setVisible(true);
                    }
                    this.setVisible(true);
                    break;
                default:
                    break;
            }
        });

        this.setSize(800, 500);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }

    public void printRectangle() {

        hypotenuseField.setVisible(false);
        panel.add(widthLabel);
        panel.add(widthField);
        panel.add(heightField);
        panel.add(buttonR);

        widthField.setVisible(true);
        heightField.setVisible(true);
        showLabel.setVisible(true);
        widthLabel.setVisible(true);
        buttonR.setVisible(true);
        buttonT.setVisible(false);

        panel.add(control);
        control.setLocation(50, 400);
        control.setPreferredSize(new Dimension(100, 50));

        widthField.setText("");
        heightField.setText("");

        showLabel.setText("");

        widthLabel.setLocation(50, 100);
        widthLabel.setSize(widthLabel.getPreferredSize());
        widthField.setLocation(widthLabel.getPreferredSize().width + 50, 100);
        widthField.setSize(widthField.getPreferredSize());
        heightField.setLocation(widthField.getPreferredSize().width + 150, 100);
        heightField.setSize(heightField.getPreferredSize());
        buttonR.setLocation(heightField.getPreferredSize().width + 230, 100);
        buttonR.setSize(buttonR.getPreferredSize());

        buttonR.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if ("".equals(widthField.getText()) && "".equals(heightField.getText())) {
                    showLabel.setText("请输入边长数据");
                } else if ("".equals(widthField.getText()) || "".equals(heightField.getText())) {
                    showLabel.setText("请输入另一边边长数据");
                } else {
                    width = Double.parseDouble(widthField.getText());
                    height = Double.parseDouble(heightField.getText());
                    Rectangle rectangle = new Rectangle(width, height);
                    boolean isRectangle = rectangle.isRectangle();
                    if (isRectangle) {
                        showLabel.setText(rectangle.printMessage());
                        rectangleDao.insertRectangle(rectangle);
                    } else {
                        showLabel.setText("图形非法!");
                    }
                }
            }
        });
    }

    /**
     * 打印矩形
     */
    public void printTriangle() {
        hypotenuseField.setVisible(true);
        panel.add(widthLabel);
        panel.add(widthField);
        panel.add(heightField);
        panel.add(hypotenuseField);
        panel.add(buttonT);

        widthField.setVisible(true);
        heightField.setVisible(true);
        hypotenuseField.setVisible(true);
        showLabel.setVisible(true);
        widthLabel.setVisible(true);
        buttonT.setVisible(true);
        buttonR.setVisible(false);

        widthField.setText("");
        heightField.setText("");
        hypotenuseField.setText("");

        showLabel.setText("");

        widthLabel.setLocation(50, 100);
        widthLabel.setSize(widthLabel.getPreferredSize());
        widthField.setLocation(widthLabel.getPreferredSize().width + 50, 100);
        widthField.setSize(widthField.getPreferredSize());
        heightField.setLocation(widthField.getPreferredSize().width + 150, 100);
        heightField.setSize(heightField.getPreferredSize());
        hypotenuseField.setLocation(heightField.getPreferredSize().width + 230, 100);
        hypotenuseField.setSize(hypotenuseField.getPreferredSize());

        buttonT.setLocation(heightField.getPreferredSize().width + 330, 100);
        buttonT.setSize(buttonT.getPreferredSize());

        showLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));

        buttonT.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if ("".equals(widthField.getText()) && "".equals(heightField.getText()) && "".equals(hypotenuseField.getText())) {
                    showLabel.setText("请输入边长数据");
                } else if("".equals(widthField.getText()) || "".equals(heightField.getText()) || "".equals(hypotenuseField.getText())) {
                    showLabel.setText("请输入其他边长数据");
                } else {
                    width = Double.parseDouble(widthField.getText());
                    height = Double.parseDouble(heightField.getText());
                    hypotenuse = Double.parseDouble(hypotenuseField.getText());
                    Triangle triangle = new Triangle(width, height, hypotenuse);
                    boolean isTriangle = triangle.isTriangle();
                    if (isTriangle) {
                        showLabel.setText(triangle.printMessage());
                        triangleDao.insertTriangle(triangle);
                    } else {
                        showLabel.setText("图形非法！");
                    }
                }
            }
        });
    }

    private class MyLayout extends SimpleLayout{

        @Override
        public void addLayoutComponent(String name, Component comp) {

        }

        @Override
        public void removeLayoutComponent(Component comp) {

        }

        @Override
        public void layoutContainer(Container parent) {
            int width = parent.getWidth();
            int height = parent.getHeight();

            label.setLocation(50, 50);
            label.setSize(label.getPreferredSize());

            comboBox.setLocation(label.getLocation().x + label.getPreferredSize().width, 50);
            comboBox.setSize(comboBox.getPreferredSize());

            showLabel.setLocation(50, 150);
            showLabel.setSize(showLabel.getPreferredSize());
        }
    }
}
